package com.zzptc.myapp1211;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.util.Log;

import com.zzptc.myapp1211.Service.MyAppService;
import com.zzptc.myapp1211.adapter.MyRvAdapter;
import com.zzptc.myapp1211.databinding.ActivityMainBinding;
import com.zzptc.myapp1211.entity.RecipeModel;
import com.zzptc.myapp1211.entity.ResultsBean;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity"; //Logt 双回车

    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    MyAppService myAppService=retrofit.create(MyAppService.class);

    ActivityMainBinding binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
       // ActivityMainBinding binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
        //setContentView(R.layout.activity_main);
        //RecyclerView rv=findViewById(R.id.rv);
        //binding.rv;
        getAllRecipe();



    }

    private void getAllRecipe() {
        Call<RecipeModel>call=myAppService.getAllRecipe(5);
       // Call<RecipeModel> allRecipe = myAppService.getAllRecipe();//只打后半部分+var
        call.enqueue(new Callback<RecipeModel>() {   //打到这里 自动会弹出以下方法 没弹出就是有问题的



            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {

                List<ResultsBean> results = response.body().getResults();

                for (ResultsBean r:
                       results){
                       r.save();
                }
                MyRvAdapter adapter=new MyRvAdapter(MainActivity.this);

                binding.rv.setAdapter(adapter);


               // Log.d(TAG, "onResponse: "+new Gson().toJson(response.body()));
            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {
              Log.e(TAG,"onFailure",t);

            }
        });




    }
}
